/*
 * @Author: Yang Zhang
 * @Date: 2021-12-01 23:21:02
 * @LastEditors: Yang Zhang
 * @LastEditTime: 2021-12-22 11:49:50
 * @Description: 
 */
import { ActionContext } from "vuex"
import { getStorageItem, setStorageItem } from "../../utils/utils"
import { IStore, IUser } from "../types"

const user = {
  state: {
    accessToken: getStorageItem('accessToken') || '',
    refreshToken: getStorageItem('refreshToken') || '',
    loginUserinfo: getStorageItem('loginUserinfo') || {},
    withdrawstatus: false
  },
  mutations: {
    SET_WITH_STATE: (state: IUser, states: boolean) => {
      state.withdrawstatus = states
    },
    SET_ACCESS_TOKEN: (state: IUser, accesstoken: string) => {
      state.accessToken = accesstoken
      setStorageItem('accessToken', accesstoken)
    },
    SET_REFRESH_TOKEN: (state: IUser, refreshToken: string) => {
      state.refreshToken = refreshToken
      setStorageItem('refreshToken', refreshToken)
    },
    SET_LOGIN_USERINFO: (state: IUser, userLogininfo: any) => {
      state.loginUserinfo = userLogininfo
      setStorageItem('loginUserinfo', userLogininfo)
    }
  },
  actions: {
    async updateHaswithdraw({ commit }: ActionContext<IUser, IStore>, status: boolean) {
      commit('SET_WITH_STATE', status)
    },
    async updateAccessToken({ commit }: ActionContext<IUser, IStore>, accesstoken: any) {
      commit('SET_ACCESS_TOKEN', accesstoken)
    },
    async updateRefreshToken({ commit }: ActionContext<IUser, IStore>, refreshtoken: any) {
      commit('SET_REFRESH_TOKEN', refreshtoken)
    },
    async updateUserinfo({ commit }: ActionContext<IUser, IStore>, userLogininfo: any) {
      commit('SET_LOGIN_USERINFO', userLogininfo)
    }
  }
}

export default user